var app = angular.module('interApp', []);
app.controller('interController', function($scope, $http, $q) {
	var bankMap={};
	
	$scope.load=function(){
		$http.get(timestamp('qz/inters/get/'+getQueryString("id"))).success(function(response){
			$scope.inter=response.data;
        });
		
		var promises = [];
		var deferred = $q.defer();
		$http.get(timestamp('qz/inters/getSpMap/'+getQueryString("id"))).success(function(response){
			deferred.resolve(response.data);
        });
		promises.push(deferred.promise);
		
		
		var deferred2 = $q.defer();
		$http.get(timestamp('sys/dic/getByGroup/yhqd')).success(function(response){
			deferred2.resolve(response.data);
        });
		promises.push(deferred2.promise);
		
		var deferred3 = $q.defer();
		$http.get(timestamp('sys/dic/getByGroup/zfklx')).success(function(response){
			deferred3.resolve(response.data);
			
        });
		promises.push(deferred3.promise);
		
		var deferred4 = $q.defer();
		$http.get(timestamp('sys/dic/getByGroup/zfksx')).success(function(response){
			deferred4.resolve(response.data);
        });
		promises.push(deferred4.promise);
		
		//attachment
//		var deferred5 = $q.defer();
		$http.get(timestamp('qz/inters/getAttchs/' + getQueryString("id"))).success(function(response){
			$scope.attachmentList=response.data;
        });
		//promises.push(deferred5.promise);
		
		$q.all(promises).then(function(datas){
			$scope.inter_sp={bankids:[]};
			
			//转换支持银行为map对象
			var bkMap={};
			$.each(datas[0].spBankList,function(i,spbank){
				bkMap[spbank.localCode]=spbank.channelCode;
				bkMap[spbank.chaIntBankId]=spbank.localCode;
				//执行选中
				$scope.inter_sp.bankids[i]=spbank.localCode;
				//银行渠道代码页面赋值
				executeTimeout(function(){
					$("#"+spbank.localCode+"_bcode").html(spbank.channelCode);
				});
			});
			console.log($scope.inter_sp);
			//---deferred2 start---
			
			//转换银行列表为map，并为已选择银行添加属性sel:true，用于默认选中
			$.each(datas[1],function(i,bank){
				bankMap[bank.dictionaryId]=bank;
				if(bkMap[bank.dictionaryId]!=null){
					datas[1][i].sel=true;
				}
			});
			$scope.bankMap=bankMap;
			
			//---deferred4 start---
			var zfksxMap={};
			$.each(datas[3],function(i,d){
				zfksxMap[d.dictionaryId]=d.name;
			});
			//---deferred3 start---
			$scope.zfklxMap={};
			$.each(datas[0].spCardList,function(j,spcard){
				$scope.zfklxMap[bkMap[spcard.spbankId]+"_"+spcard.localCode]=true;
			});
			$scope.zfklxList=datas[2];
			
			executeTimeout(function(){
				$.each(datas[0].spCardList,function(j,spcard){
					$("#"+bkMap[spcard.spbankId]+"_"+spcard.localCode+"_zfklx").html(spcard.channelCode);
					zfksxMap[spcard.chaIntCardId]={bankCode:bkMap[spcard.spbankId],cardCode:spcard.localCode}
				});
				
				
				$.each(datas[0].bcpList,function(j,bcp){
					var obj=zfksxMap[bcp.spcardId];
					var idStr=obj.bankCode+"-"+obj.cardCode;
					$("#"+idStr).append(zfksxMap[bcp.propertyId]+"&nbsp;&nbsp;");
				});
			});
			
			/*var textStr="";
			$.each(datas[0].deployList,function(j,deploy){
				textStr+=deploy.type+","+deploy.ip+","+deploy.ports+","+deploy.url+","+deploy.response+","+deploy.deploy+"\n";
			});
			$scope.deployInfo=textStr;*/
			$scope.deployInfo=datas[0].deployList;
		
		});
	}
	
	$scope.load();
	$scope.refreshData=function(){
		$scope.load();
	}
	$scope.interCancle=function(){
		redirectUrl("manage/inter/","list.jsp");
	}
	
	/**$scope.download=function(fname){
		$http({
			url: 'qz/download/filedown?filename=' + fname,
			method: "GET",
			data: 'JSON',             
			headers: {
				'Content-type': 'multipart/form-data'
			},      
			responseType: 'arraybuffer'
		}).success(function (data, status, headers, config) {		 
		    
		    var blob =  new Blob([data], {type: "applicaiton/octet-stream"});
		    var downloadUrl = window.URL.createObjectURL(blob);		   	 
		    var aLink = document.createElement('a');
		    aLink.download = fname;
		    aLink.href = downloadUrl
		    
		    var evt = document.createEvent('MouseEvents');	
		    evt.initEvent("click", false, false);
		    aLink.dispatchEvent(evt);
		    
		}).error(function (data, status, headers, config) {
			alert('error 5:'+config);
				console.log('Failed to download file: '+fname)
		});	
	}**/
	
});

function executeTimeout(fun){
	setTimeout(fun, parseInt(Math.random() * 500));
}